/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.tta;

import session.TX.TTADisplaySBRemote;
import Helper.TTAHelper.Helper_PIPSales;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import managedbean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "spipSalesMB")
@ViewScoped
public class SPIPSalesMB {

    @EJB
    TTADisplaySBRemote pipBean;
    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    private Calendar start;
    private Calendar end;
    private List<Calendar> possibleMonths;
    private Set<Integer> startMonths = new HashSet<Integer>();
    private Set<Integer> startYears = new HashSet<Integer>();
    private Set<Integer> endYears = new HashSet<Integer>();
    private Set<Integer> endMonths = new HashSet<Integer>();
    private Integer salesYearStart;
    private Integer salesMonthStart;
    private Integer salesYearEnd;
    private Integer salesMonthEnd;
    private Integer numMonthToPred = 3;

    public List<Helper_PIPSales> getSales() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        possibleMonths = this.getPossibleMonths();
        if (possibleMonths == null || possibleMonths.isEmpty()) {
            return null;
        }
        if (start == null || end == null) {
            start = possibleMonths.get(0);
            end = possibleMonths.get(possibleMonths.size() - 1);
        }

        List<Helper_PIPSales> returnList = pipBean.getSSalesData(portalManagedBean.getStakeholderName(), start, end);
        System.out.println("start:" + start.get(Calendar.YEAR) + ", " + start.get(Calendar.MONTH));
        System.out.println("end:" + end.get(Calendar.YEAR) + ", " + end.get(Calendar.MONTH));
        return returnList;
    }

    public CartesianChartModel getSalesModel() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        possibleMonths = this.getPossibleMonths();
        if (start == null || end == null) {
            start = possibleMonths.get(0);
            end = possibleMonths.get(possibleMonths.size() - 1);
        }
        List<Helper_PIPSales> returnList = pipBean.getSSalesData(portalManagedBean.getStakeholderName(), start, end);
        CartesianChartModel model = new CartesianChartModel();
        ChartSeries sales = new ChartSeries("Sales");
        for (int i = 0; i < returnList.size(); i++) {
            Helper_PIPSales aHelper = returnList.get(i);
            sales.set(aHelper.getTimeStr(), aHelper.getRevenue());
            System.out.println(aHelper.getTimeStr());
        }
        model.addSeries(sales);
        return model;
    }

    public CartesianChartModel getSalesWithPredModel() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        possibleMonths = this.getPossibleMonths();
        if (start == null || end == null) {
            start = possibleMonths.get(0);
            end = possibleMonths.get(possibleMonths.size() - 1);
        }
        List<Helper_PIPSales> returnList = pipBean.getSalesDataWithPred(portalManagedBean.getStakeholderName(),
                start, end, numMonthToPred);
        CartesianChartModel model = new CartesianChartModel();
        ChartSeries sales = new ChartSeries("Sales");
        for (int i = 0; i < returnList.size(); i++) {
            Helper_PIPSales aHelper = returnList.get(i);
            sales.set(aHelper.getTimeStr(), aHelper.getRevenue());
            System.out.println(aHelper.getTimeStr());
        }
        System.out.println("returnList size:" + returnList.size());
        model.addSeries(sales);
        return model;
    }

    public Calendar getEnd() {
        return end;
    }

    public void setEnd(Calendar end) {
        this.end = end;
    }

    public Calendar getStart() {
        return start;
    }

    public void setStart(Calendar start) {
        this.start = start;
    }

    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    public Integer getSalesMonthStart() {
        return salesMonthStart;
    }

    public void setSalesMonthStart(Integer salesMonth) {
        this.salesMonthStart = salesMonth;
    }

    public Integer getSalesYearStart() {
        return salesYearStart;
    }

    public void setSalesYearStart(Integer salesYear) {
        this.salesYearStart = salesYear;
    }

    public Integer getSalesMonthEnd() {
        return salesMonthEnd;
    }

    public void setSalesMonthEnd(Integer salesMonthEnd) {
        this.salesMonthEnd = salesMonthEnd;
    }

    public Integer getSalesYearEnd() {
        return salesYearEnd;
    }

    public void setSalesYearEnd(Integer salesYearEnd) {
        this.salesYearEnd = salesYearEnd;
    }

    public List<Calendar> getPossibleMonths() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        if (this.possibleMonths == null) {
            possibleMonths = pipBean.getSAvailableMonths(portalManagedBean.getStakeholderName());
        }
        return possibleMonths;
    }

    public Set<Integer> getStartYears() {
        if (this.startYears == null || this.startYears.isEmpty()) {
            startYears = new HashSet<Integer>();
            List<Calendar> timeList = this.getPossibleMonths();
            for (Calendar c : timeList) {
                startYears.add(c.get(Calendar.YEAR));
            }
        }
        return startYears;
    }

    public Set<Integer> getEndYears() {
        if (this.endYears == null || this.endYears.isEmpty()) {
            endYears = new HashSet<Integer>();
            List<Calendar> timeList = this.getPossibleMonths();
            for (Calendar c : timeList) {
                endYears.add(c.get(Calendar.YEAR));
            }
        }
        return endYears;
    }

    public void handleStartYearChange() {
        startMonths = new HashSet<Integer>();
        if (salesYearStart != null) {
            for (Calendar c : this.getPossibleMonths()) {
                if (c.get(Calendar.YEAR) == salesYearStart.intValue()) {
                    startMonths.add(c.get(Calendar.MONTH) + 1);
                }
            }
        }
    }

    public void handleEndYearChange() {
        endMonths = new HashSet<Integer>();
        if (salesYearEnd != null) {
            for (Calendar c : this.getPossibleMonths()) {
                if (c.get(Calendar.YEAR) == salesYearEnd.intValue()) {
                    endMonths.add(c.get(Calendar.MONTH) + 1);
                }
            }
        }
    }

    public Set<Integer> getStartMonths() {
        return startMonths;
    }

    public Set<Integer> getEndMonths() {
        return endMonths;
    }

    public void updateStartEnd(ActionEvent event) {
        start = Calendar.getInstance();
        start.set(salesYearStart, salesMonthStart - 1, 1, 0, 0, 0);
        end = Calendar.getInstance();
        end.set(salesYearEnd, salesMonthEnd - 1, 1, 0, 0, 0);
        System.out.println("start:" + start.get(Calendar.YEAR) + ", " + start.get(Calendar.MONTH));
        System.out.println("end:" + end.get(Calendar.YEAR) + ", " + end.get(Calendar.MONTH));
    }

    public Integer getNumMonthToPred() {
        return numMonthToPred;
    }

    public void setNumMonthToPred(Integer numMonthToPred) {
        this.numMonthToPred = numMonthToPred;
    }

    public void updateNumMonthToPred(ActionEvent event) {
    }
}
